/**
 * RealtimeBoardAction.java
 * Created at 2017-12-4
 * Created by zhanghailin
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.utils.DateUtils;
import com.broadtext.rms.basic.service.ITmCustomService;
import com.broadtext.rms.monitor.model.PackLocationInfo;
import com.broadtext.rms.system.service.IRealtimeBoardService;
import com.broadtext.rms.system.service.ISysUserService;

/**
 * <p>ClassName: RealtimeBoardAction</p>
 * <p>Description: 实时看板控制层</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2017-12-4</p>
 */
@Controller
@RequestMapping("/main/realtimeBoard")
public class RealtimeBoardAction {
    
    
    @Autowired
    @Qualifier("realtimeBoardService")
    private IRealtimeBoardService realtimeBoardService;
    
    /**
     * 用户Service
     */
    @Autowired
    @Qualifier("sysUserService")
    private ISysUserService sysUserService;
    
    /**
     * o 注入客户service
     */
    @Autowired
    @Qualifier("tmCustomService")
    private ITmCustomService tmCustomService;
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param packCode
     * @return
     */
    @RequestMapping(value = "/getSocData.mvc")
    @ResponseBody
    public Map<String, Object> getSocData(String packCode) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap = this.realtimeBoardService.getSocData(packCode);
        /*Date now = new Date();
        SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss");
        dataMap.put("xAxisData", f.format(now));
        dataMap.put("seriesData", Math.floor(Math.random()*100));*/
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param packCode
     * @return
     */
    @RequestMapping(value = "/getVolData.mvc")
    @ResponseBody
    public Map<String, Object> getVolData(String packCode) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap = this.realtimeBoardService.getVolData(packCode);
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param packCode
     * @return
     */
    @RequestMapping(value = "/getCurrentData.mvc")
    @ResponseBody
    public Map<String, Object> getCurrentData(String packCode) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap = this.realtimeBoardService.getCurrentData(packCode);
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param packCode
     * @return
     */
    @RequestMapping(value = "/getMaxVolData.mvc")
    @ResponseBody
    public Map<String, Object> getMaxVolData(String packCode) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap = this.realtimeBoardService.getMaxVolData(packCode);
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param packCode
     * @return
     */
    @RequestMapping(value = "/getMinVolData.mvc")
    @ResponseBody
    public Map<String, Object> getMinVolData(String packCode) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap = this.realtimeBoardService.getMinVolData(packCode);
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param packCode
     * @return
     */
    @RequestMapping(value = "/getMaxTempData.mvc")
    @ResponseBody
    public Map<String, Object> getMaxTempData(String packCode) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap = this.realtimeBoardService.getMaxTempData(packCode);
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param packCode
     * @return
     */
    @RequestMapping(value = "/getMinTempData.mvc")
    @ResponseBody
    public Map<String, Object> getMinTempData(String packCode) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap = this.realtimeBoardService.getMinTempData(packCode);
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param packCode
     * @return
     */
    @RequestMapping(value = "/getPackCodeData.mvc")
    @ResponseBody
    public List<Map<String, Object>> getPackCodeData(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        Map<String, Object> params;
        params = new HashMap<String, Object>();
        //判断登录账户类型，如果为客户账户则过滤数据，客户类型只看本级和本下级
        Map<String, Object> checkMap;
        List<String> idList;
        try {
            checkMap = this.sysUserService.checkUserClass(request.getSession().getAttribute("sys.user").toString());
            if(null != checkMap && "true".equals(checkMap.get("flag").toString())){
                //该用户是客户账号
                params.put("cusId", checkMap.get("cusId"));
                //查询当前登录用户所有下属子节点pk_id
                idList = this.tmCustomService.getChildCusIdArray(checkMap.get("cusId")+"");
                params.put("ids", idList);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        list = this.realtimeBoardService.getPackCodeData(params);
        return list;
    }
    
    /**
     * 
     * <p>
     * Description: 获取历史位置列表
     * </p>
     * 
     * @param request 请求
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value = "/getHistoryLocationList.mvc")
    @ResponseBody
    public Object getHistoryLocationList(
            @RequestParam("packCode") String packCode, @RequestParam("gatherTimeStart")String gatherTimeStart,
            @RequestParam("gatherTimeEnd") String gatherTimeEnd) {
        //处理查询条件
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        map.put("packCode", packCode);
        map.put("gatherTimeStart", gatherTimeStart);
        map.put("gatherTimeEnd", gatherTimeEnd);
        //查询
        List<PackLocationInfo> historyLocationList = new ArrayList<PackLocationInfo>();
        try {
            historyLocationList = this.realtimeBoardService.findHistoryLocationList(map);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("aaData", historyLocationList);
        return dataMap;
    }
    
    
    /**
     * 
     * <p>Description: 获取日期快捷方式</p>
     * @return
     */
    @RequestMapping(value = "/getShortcutOfDate.mvc")
    @ResponseBody
    public  Map<String, Object> getShortcutOfDate() {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //昨天开始
            String yesterdayStart = DateUtils.getYesterdayStartStr();
            map.put("yesterdayStart", yesterdayStart);
            //昨天结束
            String yesterdayEnd = DateUtils.getYesterdayEndStr();
            map.put("yesterdayEnd", yesterdayEnd);
            //上周开始
            String beginDayOfLastWeek = f.format(DateUtils.getBeginDayOfLastWeek());
            map.put("beginDayOfLastWeek", beginDayOfLastWeek);
            //上周结束
            String endDayOfLastWeek = f.format(DateUtils.getEndDayOfLastWeek());
            map.put("endDayOfLastWeek", endDayOfLastWeek);
            //上月开始
            String beginDayOfLastMonth = f.format(DateUtils.getBeginDayOfLastMonth());
            map.put("beginDayOfLastMonth", beginDayOfLastMonth);
            //上月结束
            String endDayOfLastMonth = f.format(DateUtils.getEndDayOfLastMonth());
            map.put("endDayOfLastMonth", endDayOfLastMonth);
            //上季度开始
            String beginOfLastQuarter = f.format(DateUtils.getBeginOfLastQuarter());
            map.put("beginOfLastQuarter", beginOfLastQuarter);
            //上季度结束
            String endOfLastQuarter = f.format(DateUtils.getEndOfLastQuarter());
            map.put("endOfLastQuarter", endOfLastQuarter);
            //获取去年开始
            String beginOfLastYear = f.format(DateUtils.getBeginOfLastYear());
            map.put("beginOfLastYear", beginOfLastYear);
            //获取去年结束
            String endOfLastYear = f.format(DateUtils.getEndOfLastYear());
            map.put("endOfLastYear", endOfLastYear);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return map;
        
    }
    

}
